import Vue from 'vue'
import LoadingVue from './src/index.vue'

// 继承 Vue，这是一个类
const LoadingConstructor = Vue.extend(LoadingVue)

LoadingConstructor.prototype.close = function () {
  this.visible = false
}

/**
 * @param {Object} params
 * @example 
 *  Loading.instance.show({
      theme: 'dark',
      text: '我正在保存中，请不要做其他操作',
      placement: 'center'
    })
 */
LoadingConstructor.prototype.show = function (params) {
  const defaultParams = Object.assign({
    theme: 'white',
    text: '加载中...',
    placement: 'top'
  }, params)

  this.$nextTick(() => {
    Object.assign(this, defaultParams)
    this.visible = true
    !this.infinity && setTimeout(() => {
      this.visible = false
    }, this.duration)
  })
}

// 创建 Vue 实例
const instance = new LoadingConstructor({
  name: 'Loading',
  el: document.createElement('div')
})

document.body.appendChild(instance.$el)

export default {
  install (Vue) {
    Vue.prototype.$load = instance
  },
  instance
}
